﻿using System.Collections;
using System.Windows;
using System.Windows.Controls;

namespace LpbPrj.Client.Controls
{
    public class ListBoxSelectedItems
    {
        public static IList GetSelectedItems(DependencyObject obj)
        {
            return (IList)obj.GetValue(SelectedItemsProperty);
        }

        public static void SetSelectedItems(DependencyObject obj, IList value)
        {
            obj.SetValue(SelectedItemsProperty, value);
        }

        //Using a DependencyProperty as the backing store for SelectedItems.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.RegisterAttached("SelectedItems", typeof(IList), typeof(ListBoxSelectedItems), new PropertyMetadata(OnSelectedItemsChanged));
        static public void OnSelectedItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var listBox = d as ListBox;
            if (listBox == null) return;

            //利用反射机制获取属性值
            var selectionMode = listBox.DataContext?.GetType().GetProperty("SelectionMode");
            if (selectionMode != null)
            {
                listBox.SelectionMode = (SelectionMode)selectionMode.GetValue(listBox.DataContext);
            }

            if (listBox.SelectionMode != SelectionMode.Single)
            {
                if (e.OldValue != null)
                {
                    listBox.SelectionChanged -= OnlistBoxSelectionChanged;
                }
                listBox.SelectedItems.Clear();

                IList collection = e.NewValue as IList;
                if (collection != null)
                {
                    foreach (var item in collection)
                    {
                        listBox.SelectedItems.Add(item);
                    }
                    listBox.SelectionChanged += OnlistBoxSelectionChanged;
                }
            }
        }

        static void OnlistBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            IList dataSource = GetSelectedItems(sender as DependencyObject);
            //添加用户选中的当前项.
            foreach (var item in e.AddedItems)
            {
                if (!dataSource.Contains(item)) dataSource.Add(item);
            }
            //删除用户取消选中的当前项
            foreach (var item in e.RemovedItems)
            {
                if (dataSource.Contains(item)) dataSource.Remove(item);
            }
        }

    }

    public class DataGridSelectedItems
    {
        public static IList GetSelectedItemsList(DependencyObject obj)
        {
            return (IList)obj.GetValue(SelectedItemsListProperty);
        }

        public static void SetSelectedItemsList(DependencyObject obj, IList value)
        {
            obj.SetValue(SelectedItemsListProperty, value);
        }

        static void CustomDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            IList dataSource = GetSelectedItemsList(sender as DependencyObject);
            if (dataSource == null) return;

            //添加用户选中的当前项.
            foreach (var item in e.AddedItems)
            {
                if (!dataSource.Contains(item)) dataSource.Add(item);
            }
            //删除用户取消选中的当前项
            foreach (var item in e.RemovedItems)
            {
                if (dataSource.Contains(item)) dataSource.Remove(item);
            }
        }


        public static readonly DependencyProperty SelectedItemsListProperty = DependencyProperty.RegisterAttached("SelectedItemsList", typeof(IList), typeof(DataGridSelectedItems), new PropertyMetadata(OnSelectedItemsChanged));

        static public void OnSelectedItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var dataGrid = d as DataGrid;
            if (dataGrid == null) return;

            if (dataGrid.SelectionMode != DataGridSelectionMode.Single)
            {
                if (e.OldValue != null)
                {
                    dataGrid.SelectionChanged -= CustomDataGrid_SelectionChanged;
                }

                dataGrid.SelectedItems.Clear();

                IList collection = e.NewValue as IList;
                if (collection != null)
                {
                    foreach (var item in collection)
                    {
                        dataGrid.SelectedItems.Add(item);
                    }
                    dataGrid.SelectionChanged += CustomDataGrid_SelectionChanged;
                }

            }
        }

    }

}
